var cantidadDeDiarios = 8;

window.onload = startup; 

function startup(){

	newspapers = getCookie("newspapers");

	if(newspapers == null){

		createNewspaperDictionary();
	}

	setPreferences();
	savePreferences();
}

function mostrarMenuPreferencias() {

	var preferencias = document.getElementById("menuPreferencias");
	var carousel = document.getElementById("carousel");

	if(preferencias.style.display == ""){
		carousel.style.display = "none";
		preferencias.style.display = "inline";
	} else {
		preferencias.style.display = "";
		carousel.style.display = "inline";
		savePreferences();
	}
}

function setPreferences() {

	for(var i = 0; i < cantidadDeDiarios; i++){

		if (newspapers[i] == "1") {
			document.forms["formPreferences"].checkbox[i].checked = "true";
		}
	}
}


function savePreferences() {

	newspapers = new String();

	for(var i = 0; i < cantidadDeDiarios; i++){
		if(document.forms["formPreferences"].checkbox[i].checked){
			newspapers = newspapers + "1";
		}else{
			newspapers = newspapers + "0";
		}
	}

 	setCookie("newspapers",newspapers,365);
}

function createNewspaperDictionary() {

	newspapers = new String();

	for(var i = 0; i < cantidadDeDiarios; i++){
		newspapers = newspapers + "1";
	}
}


/* Cookies */

function getCookie(c_name){
	var c_value = document.cookie;
	var c_start = c_value.indexOf(" " + c_name + "=");
	if (c_start == -1){
		c_start = c_value.indexOf(c_name + "=");
	}
	if (c_start == -1){
		c_value = null;
	} else {
		c_start = c_value.indexOf("=", c_start) + 1;
		var c_end = c_value.indexOf(";", c_start);
		if (c_end == -1){
			c_end = c_value.length;
		}
	c_value = unescape(c_value.substring(c_start,c_end));
	}
	return c_value;
}

function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

